//author:kakake
class tsAdminController {
    public data = {
        dbConn: TSLobsterAPI.DbHelper.setdbconn('42.192.250.43', 'CloudCMS', 'sa', 'Lobster@123')
    }

    public dao = new tsAdminDao();

    /**
     * 获取站点列表
     */
    getwebsitelist() {

        var pageInfo = {
            key: "WebsiteId",
            page: TSLobsterAPI.Program.RequestQuery("page", "1"),
            perPage: TSLobsterAPI.Program.RequestQuery("limit", "999"),
            totalRecord: 100
        };

        var searchstr = TSLobsterAPI.Program.RequestQuery("searchstr", "");
        var workId=TSLobsterAPI.Program.GetWorkId();

        //var data = [{ "rowid": 1, "WebsiteId": 1, "WebsiteCode": null, "Url": "https://devopen.uxhealthy.com", "Title": "互联网护理网站", "Keywords": "互联网护理网站", "Memo": "互联网护理网站", "DelFlag": 0, "WorkId": 1, "CreateTime": "2021-01-28 15:39:40", "UpdateTime": "2022-04-29 17:35:46" }, { "rowid": 2, "WebsiteId": 2, "WebsiteCode": null, "Url": "https://devopen.uxhealthy.com", "Title": "互联网医院帮助中心", "Keywords": "互联网医院帮助中心", "Memo": "互联网医院帮助中心", "DelFlag": 0, "WorkId": 1, "CreateTime": "2021-01-28 15:39:51", "UpdateTime": "2021-08-05 14:16:44" }, { "rowid": 3, "WebsiteId": 3, "WebsiteCode": null, "Url": "https://devopen.uxhealthy.com", "Title": "互联网医院健康学堂", "Keywords": "互联网医院健康学堂", "Memo": "互联网医院健康学堂", "DelFlag": 0, "WorkId": 1, "CreateTime": "2021-01-28 15:40:00", "UpdateTime": "2021-02-01 18:00:49" }, { "rowid": 4, "WebsiteId": 4, "WebsiteCode": null, "Url": "https://devopen.uxhealthy.com", "Title": "互联网医院微官网", "Keywords": "互联网医院微官网", "Memo": "互联网医院微官网", "DelFlag": 0, "WorkId": 1, "CreateTime": "2021-01-28 15:40:11", "UpdateTime": "2021-08-05 14:54:42" }, { "rowid": 5, "WebsiteId": 5, "WebsiteCode": "FollowUp", "Url": "https://devopen.uxhealthy.com", "Title": "随访系统知识库", "Keywords": "随访系统知识库", "Memo": "随访系统知识库", "DelFlag": 0, "WorkId": 1, "CreateTime": "2021-01-28 15:40:21", "UpdateTime": "2021-02-01 18:02:19" }, { "rowid": 6, "WebsiteId": 25, "WebsiteCode": null, "Url": "1", "Title": "体检科介绍", "Keywords": null, "Memo": null, "DelFlag": 0, "WorkId": 1036, "CreateTime": "2021-02-03 15:12:12", "UpdateTime": "2021-02-03 15:12:12" }, { "rowid": 7, "WebsiteId": 26, "WebsiteCode": null, "Url": "1", "Title": "测试站点2", "Keywords": "IM", "Memo": "CS", "DelFlag": 0, "WorkId": 1036, "CreateTime": "2021-02-03 16:57:43", "UpdateTime": "2022-11-14 12:24:23" }, { "rowid": 8, "WebsiteId": 27, "WebsiteCode": "FollowUp", "Url": "suifzhishiku", "Title": "随访知识库", "Keywords": null, "Memo": null, "DelFlag": 0, "WorkId": 1038, "CreateTime": "2021-02-20 18:06:28", "UpdateTime": "2021-02-20 18:06:28" }, { "rowid": 9, "WebsiteId": 1019, "WebsiteCode": "HeartExchange", "Url": "https://amispage.mcloudsoft.cn", "Title": "心交流", "Keywords": "网心康心交流", "Memo": "网心康心交流", "DelFlag": 0, "WorkId": 2049, "CreateTime": "2021-03-12 16:00:35", "UpdateTime": "2022-04-14 11:05:31" }, { "rowid": 10, "WebsiteId": 1020, "WebsiteCode": "FollowUp", "Url": "suifangxuanjiao", "Title": "随访宣教", "Keywords": null, "Memo": null, "DelFlag": 0, "WorkId": 2051, "CreateTime": "2021-03-31 11:22:47", "UpdateTime": "2021-03-31 11:22:47" }]

        return {
            list: this.dao.getwebsitelist(pageInfo, searchstr,workId),
            //list:data,
            count: pageInfo.totalRecord
        };
    }

    savewebsite() {
        var website = JSON.parse(TSLobsterAPI.Program.RequestBody());
        website.WorkId = TSLobsterAPI.Program.GetWorkId();
        if (website.websiteId == 0) {
            this.dao.insertwebsite(website);
        }
        else {
            this.dao.updatewebsite(website);
        }

        return { result: true };
    }

    setwebsitestatus() {
        var param = {
            websiteId: TSLobsterAPI.Program.RequestQuery("websiteId", "0"),
            delFlag: TSLobsterAPI.Program.RequestQuery("delFlag", "0")
        }
        this.dao.setwebsitestatus(param);
        return { result: true };
    }

    getnodelist() {
        var param = {
            websiteid: TSLobsterAPI.Program.RequestQuery("websiteId", "0")
        }
        var data = this.dao.getnodelist(param);
        return { data: data, count: data.length };
    }

    disablenode() {
        var param = {
            nodeid: TSLobsterAPI.Program.RequestQuery("nid", "0"),
            status: TSLobsterAPI.Program.RequestQuery("status", "0")
        }

        this.dao.disablenode(param);
        return { result: true };
    }

    deletenode() {
        var param = {
            nodeid: TSLobsterAPI.Program.RequestQuery("nid", "0"),
        }

        this.dao.deletenode(param);
        return { result: true };
    }

    movenode() {
        var param = {
            nodeid: TSLobsterAPI.Program.RequestQuery("nid", "0"),
            movestatus: parseInt(TSLobsterAPI.Program.RequestQuery("moveStatus", "0")),
            oldsort: 0,
            parentid: 0,
            WebsiteId: 0,
            workid: TSLobsterAPI.Program.GetWorkId()
        }
        TSLobsterAPI.Utils.TrackLog('param', param);
        var curnode = this.dao.getnode(param);
        param.oldsort = curnode.NodeSort;
        param.parentid = curnode.ParentID;
        param.WebsiteId = curnode.WebsiteId;
        TSLobsterAPI.Utils.TrackLog('param', param);
        var exchangenode = this.dao.getexchangenode(param);
        TSLobsterAPI.Utils.TrackLog('exchangenode', exchangenode);
        if (!exchangenode.NID) {
            if (param.movestatus == 0) {
                throw "首行数据不能上移"
            } else {
                throw "尾行数据不能下移";
            }
        }
        else {
            this.dao.updatenodesort({ nodeid: curnode.NID, nodesort: exchangenode.NodeSort });
            this.dao.updatenodesort({ nodeid: exchangenode.NID, nodesort: param.oldsort });
        }

        return { result: true };
    }


    savenode() {
        var node = JSON.parse(TSLobsterAPI.Program.RequestBody());
        if (node.NID == 0) {
            this.dao.insertnode(node);
        } else {
            this.dao.updatenode(node);
        }
        return { result: true };
    }

    getnodeandparentnode() {
        var nodeid = parseInt(TSLobsterAPI.Program.RequestQuery("nid", "0"));
        var data = this.dao.getnodeandparentnode(nodeid);
        return { data: data };
    }

    getcontentlist() {
        var pageInfo = {
            key: "CID desc",
            page: TSLobsterAPI.Program.RequestQuery("page", "1"),
            perPage: TSLobsterAPI.Program.RequestQuery("limit", "999"),
            totalRecord: 100
        };

        var searchstr = TSLobsterAPI.Program.RequestQuery("key", "");
        var nodeid = TSLobsterAPI.Program.RequestQuery("nid", "0");
        var data = this.dao.getcontentlist(searchstr, nodeid, pageInfo);

        return { list: data, count: pageInfo.totalRecord };
    }

    updatecontextstate() {
        var cid = parseInt(TSLobsterAPI.Program.RequestQuery("cid", "0"));
        var state = parseInt(TSLobsterAPI.Program.RequestQuery("state", "0"));
        this.dao.updatecontextstate(cid, state);

        return { result: true };
    }

    deletecontext() {
        var cid = parseInt(TSLobsterAPI.Program.RequestQuery("cid", "0"));
        this.dao.deletecontext(cid);

        return { result: true };
    }

    getarticle() {
        var cid = parseInt(TSLobsterAPI.Program.RequestQuery("cid", "0"));
        var data = this.dao.getarticle(cid);

        return { data: data };
    }

    savecontext() {
        var context = JSON.parse(TSLobsterAPI.Program.RequestBody());
        //新增
        if (context.CID == 0) {
            this.dao.insertcontext(context);
        }
        else {
            this.dao.updatecontext(context);
        }
        return { result: true };
    }

    getIndexSetting() {
        var websiteId = TSLobsterAPI.Program.RequestQuery("websiteId", "0");

        var pageInfo = {
            key: "sort",
            page: TSLobsterAPI.Program.RequestQuery("page", "1"),
            perPage: TSLobsterAPI.Program.RequestQuery("limit", "999"),
            totalRecord: 100
        };

        var data = this.dao.getIndexSetting(websiteId, pageInfo);

        return { data: data, count: pageInfo.totalRecord };
    }

    getWebSiteListData() {
        var workId=TSLobsterAPI.Program.GetWorkId();
        var data = this.dao.getWebSiteListData(workId);
        return { data: data };
    }

    saveindexsetting() {
        var setting = JSON.parse(TSLobsterAPI.Program.RequestBody());
        setting.WorkId = TSLobsterAPI.Program.GetWorkId();
        if (setting.Id == 0) {
            this.dao.insertindexsetting(setting);
        }
        else {
            this.dao.updateindexsetting(setting);
        }
        return { result: true };
    }

    setsettingstatus() {
        var id = TSLobsterAPI.Program.RequestQuery("id", "0");
        var delflag = TSLobsterAPI.Program.RequestQuery("delflag", "0");
        this.dao.setsettingstatus(id, delflag);
        return { result: true };
    }

    getcontextlistbywebsiteId() {
        var websiteId = TSLobsterAPI.Program.RequestQuery("websiteId", "0");
        var data = this.dao.getcontextlistbywebsiteId(websiteId);
        return { data: data };
    }

    getnodelistbywebsiteId() {
        var websiteId = TSLobsterAPI.Program.RequestQuery("websiteId", "0");
        var data = this.dao.getnodelistbywebsiteId(websiteId);
        return { data: data };
    }

}